//////////////////////////////////////////////////////////////////////
//
// File: BananasSample.java
//
// Copyright (c) 2005 TiVo Inc.
//
//////////////////////////////////////////////////////////////////////

package com.tivo.hme.samples.bananas;

import com.tivo.hme.bananas.BApplicationPlus;
import com.tivo.hme.bananas.BView;
import com.tivo.hme.sdk.HmeEvent;
import java.awt.Color;
import java.awt.Rectangle;

/**
 * A sample application to show off the features of the Bananas toolkit
 *
 * @author Carl Haynes
 */
public class BananasPlusSample extends BApplicationPlus
{
    //
    // This is the title of the application. The runtime environment will
    // discover the title here and use it as the name of the application.
    //
    
    public final static String TITLE = "Bananas Sample";
    
    private boolean simulator = false;
    private BView innerSimView;
    
    /**
     * Show the main screen for our sample app, as this is the first screen
     * shown, we will not show a transition to this screen
     */
    public void init() 
    {
    	super.init();
        
        setBackground();

        push(new MainMenuScreen(this), TRANSITION_NONE);
    }
    
    
    @Override
    public void rootBoundsChanged(Rectangle rootBounds) {
        super.rootBoundsChanged(rootBounds);
        setBackground();
    }
    
    protected void setBackground() {
        //
        // If we are running in the simulator display jpg.
        //
        if (simulator) {
            getRoot().setResource(new Color(0, 0, 55));
            
            int offset = (getCurrentResolution().getHeight() == 720) ? 54 : 40;
            
            int safeTitleH = getSafeTitleHorizontal();
            int safeTitleV = getSafeTitleVertical();
            if (innerSimView == null) {
                innerSimView = new BView(getBelow(), safeTitleH, safeTitleV+offset, 
                        getWidth()-safeTitleH, getHeight()-2*safeTitleV-offset);
            } else {
                innerSimView.setBounds(safeTitleH, safeTitleV+offset, 
                        getWidth()-safeTitleH, getHeight()-2*safeTitleV-offset);
            }
            innerSimView.setResource(new Color(0, 0, 33));
        } else {
            getBelow().setResource("blue.mpg");
        }
    }


    /**
     * Every screen handle the "pop" action in the same way, so we will handle
     * this in the application.
     */
    public boolean handleAction(BView view, Object action) 
    {
        if (action.equals("pop")) {
            pop();
            return true;
        }        
        return super.handleAction(view, action);
    }
    
    /**
     * This event handler looks for device info in order to substitute a jpeg
     * image as a background instead of the mpeg. Without this the root view in
     * the simulator the root would be left with a black background.
     *
     * This code and extra jpeg image are meant to be removed from a shipping
     * application!
     *
     * REMIND: This is only needed by developers while using the simulator!
     */
    public boolean handleEvent(HmeEvent event)
    {
        switch (event.getOpCode()) {
          case EVT_DEVICE_INFO:
            HmeEvent.DeviceInfo info = (HmeEvent.DeviceInfo)event;
            simulator =((String)info.getMap().get("platform")).startsWith("sim-");
            setBackground();
            break;
        }
        return super.handleEvent(event);
    }
    
}
